#include <REGX52.H>
#include "Delay.h"
#include "Key.h"
#include "LCD1602.h"
#include "AT24C02.h"


unsigned char KeyNum;
unsigned int Num;


void main()
{
	LCD_Init();			   //LCD初始化
	
//	AT24C02_WriteByte(1,123);	//在字地址写入数据
//	Delay(5);					//延时5ms，因为AT24C02内部会进行一些工作
//	Data = AT24C02_ReadByte(1);	//读取数据
//	
	LCD_ShowNum(1,1,Num,5);//显示数据

	while(1)
	{
		KeyNum = Key();//获取按键
		if (1 == KeyNum) 
		{
			Num++;
			LCD_ShowNum(1,1,Num,5);//更新显示
		}
		if (2 == KeyNum)
		{
			Num--;
			LCD_ShowNum(1,1,Num,5);//更新显示
		}
		if (3 == KeyNum)
		{
			AT24C02_WriteByte(0,Num % 256);//在字地址0写入数据的低8位
			Delay(5);				//保证写入成功
			
			AT24C02_WriteByte(1,Num / 256);//在字地址1写入数据的高8位
			Delay(5);				//保证写入成功
			
			LCD_ShowString(2,1,"Write OK!");//提示写入成功
			Delay(1000);
			LCD_ShowString(2,1,"         ");//清理
		}
		if (4 == KeyNum)
		{
			Num = AT24C02_ReadByte(0);	//读取数据低8位
			Num |= AT24C02_ReadByte(1) << 8;//读取数据高8位并不改变低8位
			
			LCD_ShowNum(1,1,Num,5);//更新显示
	
			LCD_ShowString(2,1,"Read OK!");//提示读取成功
			Delay(1000);
			LCD_ShowString(2,1,"        ");//清理
		}
	}
}

























